
#include "PolarAlign.h"
#include "ui_PolarAlign.h"
#include "ui_Servers.h"

#include <Qt/qinputdialog.h>
#include <QString>
#include <typeinfo>

#include <IV22.h>
#include <BSC.h>
#include <KStars.h>

PolarAlign::PolarAlign(): QMainWindow()
{
    ui=new Ui::PolarAlign();
    ui->setupUi(this);
    uiservers=new Ui::Servers();
    uiservers->setupUi(&dialogServer);

    hc=new HandController();    
    hc->setParent(ui->dockWidgetContents);
    hc->setWindowFlags(Qt::Widget);
    hc->show();
    //ui->dockWidgetpad->setWidget(hc);
    //hc->show();
   
    mountdevice=NULL;
    connect(ui->actionConnect_Server, SIGNAL(triggered()), this, SLOT(connectServer()));
    connect(ui->actionConnect_Mount, SIGNAL(triggered()), this, SLOT(connectMountDevice()));
    connect(hc, SIGNAL(serverconnected()), this, SLOT(serverConnected()));
    connect(hc, SIGNAL(serverdisconnected(int)), this, SLOT(serverDisconnected(int)));
    
    connect(hc, SIGNAL(observerchanged(double,double)), this, SLOT(changeObserver(double,double)));

    
    //cat=new Iv22();
    //cat->setDatafileName("/nfs/devel/indi-tools-import/Catalogs/catalogs/IV22/index.dat");
    cat=new Bsc();
    cat->setDatafileName("/nfs/devel/indi-tools-import/Catalogs/catalogs/bsc/bsc5.dat");    
    //cat=new KStars();
    //cat->setDatafileName("/usr/share/kde4/apps/kstars/stars.dat");    
    cat->loadData();
    tswizard = new TwoStarWizard(ui->centralwidget);
    tswizard->setModal(false);
    starpairs=new StarSelect();
    starpairs->setCatalog(cat);
    tswizard->setStarPairs(starpairs);
    tswizard->setClient(hc);
    connect(hc, SIGNAL(mountconnected(INDI::BaseDevice*)), tswizard, SLOT(setTelescope(INDI::BaseDevice *)));
    connect(hc,SIGNAL(observerchanged(double,double)), starpairs, SLOT(setSite(double,double)));
    connect(hc, SIGNAL(lstchanged(double)), starpairs, SLOT(setLST(double)));
    tswizard->show();
}

//PolarAlign::~PolarAlign()
//{}

bool PolarAlign::connectServer()
{
  //std::printf("Connecting...");
  //uiservers->show();
  uiservers->lineEditcustomhost->setText(QString("localhost"));
  uiservers->lineEditcustomport->setText(QString("7624"));
  if (dialogServer.exec() == QDialog::Accepted) {
    hc->setServer(uiservers->lineEditcustomhost->text().toAscii().data(), uiservers->lineEditcustomport->text().toUInt());
    IDLog("Connecting...\n");	
    hc->connectServer();
  }
}

bool PolarAlign::disconnectServer()
{
  //std::printf("Connecting...");
  IDLog("Disconnecting...\n");
  hc->disconnectServer();
}

bool PolarAlign::connectMountDevice()
{
  QStringList deviceList;
  bool ok;
  //QInputDialog *dialogDevice;
  //dialogDevice = new QInputDialog(this);
 
  foreach(INDI::BaseDevice *dp, hc->getDevices()) {  
    //printf("Device type %s %c\n", typeid(*dp).name(), typeid(INDI::Telescope).before(typeid(*dp)) ? '1':'0');
    deviceList.append(dp->getDeviceName());
  }
  QString deviceName=QInputDialog::getItem(this, tr("Select Mount device"), tr("Mount Device"), deviceList, 0, false, &ok);
  if (ok) {
    mountdevice=hc->getDevice(deviceName.toAscii().data());
 
    IDLog("Connecting device %s...\n", mountdevice->getDeviceName());
    mountwasconnected=false;
    //if (not mountdevice->isConnected()) {
    //  INDI::BaseClient::connectDevice(mountdevice->getDeviceName());
    //} else {
    // mountwasconnected=true; 
    //}
    if (mountdevice->isConnected()) mountwasconnected=true; 
    hc->connectDevice(mountdevice->getDeviceName());
    /* give craps, should wait maybe */
    
  }
}

bool PolarAlign::disconnectMountDevice()
{
  
  if ((mountdevice) && (not mountwasconnected)){
    IDLog("Disconnecting %s...\n", mountdevice->getDeviceName());
    hc->disconnectDevice(mountdevice->getDeviceName());
  }
  mountdevice=NULL;
  ui->actionConnect_Mount->setText(tr("Connect Mount"));
  QObject::disconnect(ui->actionConnect_Mount, SIGNAL(triggered()), this, SLOT(disconnectMountDevice()));
  QObject::connect(ui->actionConnect_Mount, SIGNAL(triggered()), this, SLOT(connectMountDevice())); 
  
}

void PolarAlign::changeObserver(double lng, double lat)
{
  starpairs->setSite(obs_lon, obs_lat);
}


void PolarAlign::newDevice(INDI::BaseDevice *dp)
{
  //std::printf("new device %s\n", dp->getDeviceName());
  IDLog("new device %s\n", dp->getDeviceName());
  //ui->comboBoxDevice->addItem(dp->getDeviceName(), QVariant::fromValue<INDI::BaseDevice *>(dp));
}

void PolarAlign::newProperty(INDI::Property* property)
{
 
}

void PolarAlign::removeProperty(INDI::Property *property) 
{
  
}

void PolarAlign::newBLOB(IBLOB *bp)
{

}

void PolarAlign::newSwitch(ISwitchVectorProperty *svp) 
{
  
}

void PolarAlign::newNumber(INumberVectorProperty *nvp)
{

  
}

void PolarAlign::newMessage(INDI::BaseDevice *dp, int messageID)
{
  
}

void PolarAlign::newText(ITextVectorProperty *tvp)
{
  
}

void PolarAlign::newLight(ILightVectorProperty *lvp)
{
  
}

void PolarAlign::serverConnected()
{
  
  ui->actionConnect_Server->setText("Disconnect " + QString(hc->getHost()));
  QObject::disconnect(ui->actionConnect_Server, SIGNAL(triggered()), this, SLOT(connectServer()));
  QObject::connect(ui->actionConnect_Server, SIGNAL(triggered()), this, SLOT(disconnectServer()));
  ui->actionConnect_Mount->setEnabled(true);
  //ui->actionConnect_CCD->setEnabled(true);
  
}

void PolarAlign::serverDisconnected(int exit_code)
{
  ui->actionConnect_Server->setText("Connect server");
  QObject::disconnect(ui->actionConnect_Server, SIGNAL(triggered()), this, SLOT(disconnectServer()));
  QObject::connect(ui->actionConnect_Server, SIGNAL(triggered()), this, SLOT(connectServer()));
  ui->actionConnect_Mount->setEnabled(false);

}  


